﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Parallel3M.parallel3m.framework
{
    public class ParallelTransfer:ParallelProcessor
    {

        public override void process()
        {
            foreach (ParallelConnection inputConnection in inputConnections)
            {
                ParallelPayload payload = inputConnection.Flowout();
                if (payload != null)
                {
                    payloadcount++;

                    //handle the payload and generate new payload to the output connection
                    if(payload.routeCount > 0) {
                        payload.routeCount--;
                    }
                    ArrayList payloads = execute(payload);

                    FlowoutPolicy(payloads, outputConnections);
                }
            }
        }

        public virtual ArrayList execute(ParallelPayload payload)
        {
            return null;
        }

        public virtual void FlowoutPolicy(ArrayList payloads, ArrayList outputConnections)
        {
            if (payloads != null)
            {
                foreach (ParallelPayload output_payload in payloads)
                {
                    foreach (ParallelConnection outputConnection in outputConnections)
                    {
                        outputConnection.Flowin(output_payload);
                        break;

                    }
                }                
            }
        }
    }
}
